import { mapState } from 'vuex'
var data = {
    messages: [],
    pageNo: 0,
    pageSize: 10,
    count: 99,
    refreshing: false
}
var vm = {
    data() {
        return data
    },
    computed: {
        ...mapState(['user']),
        noMoreMessage() {
            return this.messages != null && this.messages.length >= this.count
        }
    },
    mounted() {
        if (this.pageNo == 0) this.loadMoreMessage()
        $ajax.user.messageRead({
            userId: this.user.userId
        })
    },
    methods: {
        // 记载更多消息
        loadMoreMessage() {
            this.pageNo++
            if (!this.noMoreMessage) {
                $ajax.user.messageList({
                    userId: $store.state.user.userId,
                    pageNo: this.pageNo,
                    pageSize: this.pageSize
                }).then(res => {
                    var { code, data } = res.data
                    if (code == 'S') {
                        if (this.pageNo > 1) {
                            this.messages.push(...data.list)
                        } else {
                            this.messages = data.list
                        }
                        this.count = data.count
                    } else {
                        this.pageNo--
                    }
                    this.closeMessageLoad()
                }).catch(err => {
                    this.pageNo--
                    this.closeMessageLoad()
                })
            }
        },
        // 重新加载消息
        reLoadMessage() {
            this.pageNo = 0
            this.count = 99
            this.refreshing = true
            this.loadMoreMessage()
        },
        // 关闭消息加载
        closeMessageLoad() {
            this.refreshing = false
            this.$refs.messageScroller.infiniteDone()
            this.$refs.messageScroller.refreshDone()
        }
    },
    mixins: [$mixins.needLogin]
}
export default vm